﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WangMiEntity.EmployeeService;
using WangMiEntity.Order;
using WangMiEntity.GridViewModel;
using WangMiBusiness.BusinessConstant;
using TaoBaoServices;

namespace WangMiBusiness.GridViewModel
{
    public class GridViewHelper
    {
        /// <summary>
        /// 补齐所有日期数据
        /// </summary>
        /// <param name="itemList"></param>
        /// <returns></returns>
        public static List<XiaoShoueDateEntity> GetAllDate(DateTime startTime, DateTime endTime, List<XiaoShoueDateEntity> itemList)
        {
            int days = Convert.ToInt32((endTime - startTime).TotalDays);

            for (int i = days; i >= 0; i--)
            {
                string date = startTime.AddDays(i).ToShortDateString();
                bool isDate = itemList.Exists(item => item.OrderDate == date);
                if (!isDate)
                {
                    XiaoShoueDateEntity xiaoShoueDateEntity = new XiaoShoueDateEntity();
                    xiaoShoueDateEntity.OrderDate = date;

                    itemList.Add(xiaoShoueDateEntity);
                }
            }

            return itemList;
        }

        /// <summary>
        /// 获取订单价格列表
        /// </summary>
        /// <param name="baseGridModel"></param>
        /// <param name="sellerNick"></param>
        /// <returns></returns>
        public static List<OrderPrice> GetOrderPrice(List<BaseGridModel> baseGridModel, string sellerNick)
        {
            List<OrderPrice> orderPriceList = new List<OrderPrice>();

            baseGridModel.ForEach(item =>
            {
                double refundingPrice = 0;
                double refundPrice = 0;

                int paymentOrderCount = 0;
                int refundingOrderCount = 0;
                int refundOrderCount = 0;

                OrderPrice orderPrice = new OrderPrice();
                orderPrice.OrderDate = item.OrderDate;

                item.SellerConsignedPartModel.ForEach(p =>
                {
                    orderPrice.SellerConsignedPartPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.SellerConsignedPartOrderCount += paymentOrderCount;
                });
                orderPrice.SellerConsignedPartTradeCount = item.SellerConsignedPartModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.SELLER_CONSIGNED_PART));

                item.TradeBuyerSignedModel.ForEach(p =>
                {
                    orderPrice.TradeBuyerSignedPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.TradeBuyerSignedOrderCount += paymentOrderCount;
                });
                orderPrice.TradeBuyerSignedTradeCount = item.TradeBuyerSignedModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.TRADE_BUYER_SIGNED));

                item.TradeClosedByTaoBaoModel.ForEach(p =>
                {
                    orderPrice.TradeClosedByTaoBaoPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.TradeClosedByTaoBaoOrderCount += paymentOrderCount;
                });
                orderPrice.TradeClosedByTaoBaoTradeCount = item.TradeClosedByTaoBaoModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.TRADE_CLOSED_BY_TAOBAO));

                item.TradeClosedModel.ForEach(p =>
                {
                    orderPrice.TradeClosedPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.TradeClosedOrderCount += paymentOrderCount;
                });
                orderPrice.TradeClosedTradeCount = item.TradeClosedModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.TRADE_CLOSED));

                item.TradeFinishedModel.ForEach(p =>
                {
                    orderPrice.TradeFinishedPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.TradeFinishedOrderCount += paymentOrderCount;
                });
                orderPrice.TradeFinishedTradeCount = item.TradeFinishedModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.TRADE_FINISHED));

                item.TradeNoCreatePayModel.ForEach(p =>
                {
                    orderPrice.TradeNoCreatePayPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.TradeNoCreatePayOrderCount += paymentOrderCount;
                });
                orderPrice.TradeNoCreatePayTradeCount = item.TradeNoCreatePayModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.TRADE_NO_CREATE_PAY));

                item.WaitBuyerConfirmGoodsModel.ForEach(p =>
                {
                    orderPrice.WaitBuyerConfirmGoodsPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.WaitBuyerConfirmGoodsOrderCount += paymentOrderCount;
                });
                orderPrice.WaitBuyerConfirmGoodsTradeCount = item.WaitBuyerConfirmGoodsModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.WAIT_BUYER_CONFIRM_GOODS));

                item.WaitBuyerPayModel.ForEach(p =>
                {
                    orderPrice.WaitBuyerPayPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.WaitBuyerPayOrderCount += paymentOrderCount;
                });
                orderPrice.WaitBuyerPayTradeCount = item.WaitBuyerPayModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.WAIT_BUYER_PAY));

                item.WaitSellerSendGoodsModel.ForEach(p =>
                {
                    orderPrice.WaitSellerSendGoodsPrice += GetPayment(p, sellerNick, ref refundingPrice, ref refundPrice, ref paymentOrderCount, ref refundingOrderCount, ref refundOrderCount);
                    orderPrice.WaitSellerSendGoodsOrderCount += paymentOrderCount;
                });
                orderPrice.WaitSellerSendGoodsTradeCount = item.WaitSellerSendGoodsModel.Count(n => IsTypeMatch(n, sellerNick, OrderStatus.WAIT_SELLER_SEND_GOODS));

                orderPrice.RefundingPrice = refundingPrice;
                orderPrice.RefundPrice = refundPrice;

                orderPrice.RefundingTradeCount = GetRefundingCount(item);
                orderPrice.RefundTradeCount = GetRefundCount(item);

                orderPrice.RefundingOrderCount = refundingOrderCount;
                orderPrice.RefundOrderCount = refundOrderCount;

                orderPriceList.Add(orderPrice);
            });

            return orderPriceList;
        }

        /// <summary>
        /// 获取子订单数
        /// </summary>
        /// <param name="baseOrderModel"></param>
        /// <returns></returns>
        protected static int GetOrderCount(List<BaseOrderModel> baseOrderModelList, ref int refundingOrderCount, ref int refundOrderCount)
        {
            int count = 0;

            foreach (var item in baseOrderModelList)
            {
                foreach (var n in item.Trade.Orders)
                {
                    if (n.RefundStatus == RefundStatus.NO_REFUND)
                    {
                        count++;
                    }
                    else if (n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER)
                    {
                        refundingOrderCount++;
                    }
                    else if (n.RefundStatus == RefundStatus.SUCCESS)
                    {
                        refundOrderCount++;
                    }
                }
            }

            return count;
        }

        /// <summary>
        /// 获取价格
        /// </summary>
        /// <param name="baseOrderModel"></param>
        /// <param name="sellerNick"></param>
        /// <param name="refundingPrice"></param>
        /// <param name="refundPrice"></param>
        /// <param name="paymentTradeCount"></param>
        /// <param name="refundingOrderCount"></param>
        /// <param name="refundOrderCount"></param>
        /// <returns></returns>
        protected static double GetPayment(BaseOrderModel baseOrderModel, string sellerNick, ref double refundingPrice, ref double refundPrice, ref int paymentOrderCount, ref int refundingOrderCount, ref int refundOrderCount)
        {
            double payment = 0;
            double paymenttemp = 0;

            if (baseOrderModel != null && baseOrderModel.Trade != null && baseOrderModel.Trade.Orders.Count > 0)
            {
                foreach (var n in baseOrderModel.Trade.Orders)
                {
                    if (n.RefundStatus == RefundStatus.NO_REFUND)
                    {
                        if (string.IsNullOrEmpty(sellerNick))
                        {
                            double.TryParse(n.Payment, out paymenttemp);
                            payment += paymenttemp;
                            paymentOrderCount++;
                        }
                        else
                        {
                            if (baseOrderModel.Trade.SellerNick == sellerNick)
                            {
                                double.TryParse(n.Payment, out paymenttemp);
                                payment += paymenttemp;
                                paymentOrderCount++;
                            }
                        }
                    }
                    else if (n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER)
                    {
                        if (string.IsNullOrEmpty(sellerNick))
                        {
                            double.TryParse(n.Payment, out paymenttemp);
                            refundingPrice += paymenttemp;
                            refundingOrderCount++;
                        }
                        else
                        {
                            if (baseOrderModel.Trade.SellerNick == sellerNick)
                            {
                                double.TryParse(n.Payment, out paymenttemp);
                                refundingPrice += paymenttemp;
                                refundingOrderCount++;
                            }
                        }
                    }
                    else if (n.RefundStatus == RefundStatus.SUCCESS)
                    {
                        if (string.IsNullOrEmpty(sellerNick))
                        {
                            double.TryParse(n.Payment, out paymenttemp);
                            refundPrice += paymenttemp;
                            refundOrderCount++;
                        }
                        else
                        {
                            if (baseOrderModel.Trade.SellerNick == sellerNick)
                            {
                                double.TryParse(n.Payment, out paymenttemp);
                                refundPrice += paymenttemp;
                                refundOrderCount++;
                            }
                        }
                    }
                }
            }

            return payment;
        }


        /// <summary>
        /// 获取满意度
        /// </summary>
        /// <param name="baseOrderModel"></param>
        /// <param name="refundingPrice"></param>
        /// <param name="refundPrice"></param>
        /// <returns></returns>
        protected static List<ManYiDuDateEntity> GetManYiDu(string sessionKey)
        {
            List<ManYiDuDateEntity> manYiDuDateEntityList = new List<ManYiDuDateEntity>();
            var gridViewModel = new GridViewModelBusiness().GetTradeRateViewModel(sessionKey);

            gridViewModel.ForEach(item =>
            {
                ManYiDuDateEntity manYiDuDateEntity = new ManYiDuDateEntity();
                manYiDuDateEntity.OrderDate = item.OrderDate;
                manYiDuDateEntity.DefaultGoodComment = item.TradeRateList.Count(n =>
                {
                    if (n.Result == APIDescriptionConstant.TraderateGoodComment && n.Content.Contains(APIDescriptionConstant.TraderateDefaultComment))
                        return true;
                    return false;

                });
                manYiDuDateEntity.NonDefaultGoodComment = item.TradeRateList.Count(n =>
                {
                    if (n.Result == APIDescriptionConstant.TraderateGoodComment && n.Content.Contains(APIDescriptionConstant.TraderateDefaultComment))
                        return true;
                    return false;

                });
                manYiDuDateEntity.DefaultGoodComment = item.TradeRateList.Count(n =>
                {
                    if (n.Result == APIDescriptionConstant.TraderateGoodComment && n.Content.Contains(APIDescriptionConstant.TraderateDefaultComment))
                        return true;
                    return false;

                });
            });
            return manYiDuDateEntityList;
        }

        /// <summary>
        /// 是否是当前Status的List
        /// </summary>
        /// <param name="baseOrderModel"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        protected static bool IsTypeMatch(BaseOrderModel baseOrderModel, string sellerNick, string status)
        {
            if (baseOrderModel != null && baseOrderModel.Trade != null && baseOrderModel.Trade.Status == status && baseOrderModel.Trade.SellerNick == sellerNick && baseOrderModel.Trade.Orders.Exists(p => p.RefundStatus == RefundStatus.NO_REFUND))
                return true;
            return false;
        }

        /// <summary>
        /// 获取退款中交易数
        /// </summary>
        /// <param name="baseGridModel"></param>
        /// <returns></returns>
        protected static int GetRefundingCount(BaseGridModel baseGridModel)
        {
            int count = 0;
            count += baseGridModel.WaitSellerSendGoodsModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER));
            count += baseGridModel.SellerConsignedPartModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER));
            count += baseGridModel.WaitBuyerConfirmGoodsModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER));
            count += baseGridModel.TradeBuyerSignedModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER));
            count += baseGridModel.TradeFinishedModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER));
            count += baseGridModel.TradeClosedModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.WAIT_SELLER_AGREE || n.RefundStatus == RefundStatus.WAIT_BUYER_RETURN_GOODS || n.RefundStatus == RefundStatus.WAIT_SELLER_CONFIRM_GOODS || n.RefundStatus == RefundStatus.SELLER_REFUSE_BUYER));

            return count;
        }

        /// <summary>
        /// 获取已退款交易数
        /// </summary>
        /// <param name="baseGridModel"></param>
        /// <returns></returns>
        protected static int GetRefundCount(BaseGridModel baseGridModel)
        {
            int count = 0;
            count += baseGridModel.WaitSellerSendGoodsModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.SUCCESS));
            count += baseGridModel.SellerConsignedPartModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.SUCCESS));
            count += baseGridModel.WaitBuyerConfirmGoodsModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.SUCCESS));
            count += baseGridModel.TradeBuyerSignedModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.SUCCESS));
            count += baseGridModel.TradeFinishedModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.SUCCESS));
            count += baseGridModel.TradeClosedModel.Count(p => p.Trade.Orders.Exists(n => n.RefundStatus == RefundStatus.SUCCESS));

            return count;
        }
    }
}
